﻿// DlgCDCheck.cpp : 实现文件
//

#include "stdafx.h"
#include "POSCheck.h"
#include "DlgCDCheck.h"
#include "Cashdrawer.h"

// CDlgCDCheck 对话框

//IMPLEMENT_DYNAMIC(CDlgCDCheck, CDialog)
using namespace Utility::LoggerLib;
CDlgCDCheck::CDlgCDCheck(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgCDCheck::IDD, pParent)
{
    m_pCashdrawer = NULL;

    m_bPT = FALSE;
    m_nStatus = ES_Unchecked;
}

CDlgCDCheck::~CDlgCDCheck()
{
    if (NULL != m_pCashdrawer)
    {
        delete m_pCashdrawer;
    }
//	KillTimer(1);
}

void CDlgCDCheck::SetCashdrawerInfo(int nPort, int nType /* = EComCashdrawer */, int nMBType /* = EUnkownMainboard */, int nExtrInfo /* = 0 */)
{
    switch(nType)
    {
    case  EComCashdrawer:
        m_pCashdrawer = new CComCashdrawer(nPort);
        _QLOG_DEBUG;
        break;
    case EPrinterwithCashdrawer:
        m_pCashdrawer = new CPTCashdrawer(nPort);
        _QLOG_DEBUG;
        break;
	case EMBCashdrawer:
		{
			switch(nMBType)
			{
			case N2600:
				m_pCashdrawer = new CMBCashdrawer_N2600();
                _QLOG_DEBUG;
				break;

            case N2600JF:
                m_pCashdrawer = new CMBCashdrawer_N2600_JF(nExtrInfo);
                _QLOG_DEBUG;
                break;

			case D525:
			default:
				m_pCashdrawer = new CMBCashdrawer(D525);
                _QLOG_DEBUG;
				break;
				
			}
		}
		
		break;
    case ELDwithCashdrawer:
    default:
        break;;
    }
}

void CDlgCDCheck::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CDlgCDCheck::OnInitDialog()
{
    CDialog::OnInitDialog();

    GetDlgItem(IDC_RADIO_CD_NORMAL)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_CD_ERROR)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_CD_PASS)->EnableWindow(m_bPT);
    GetDlgItem(IDOK)->EnableWindow(!m_bPT);

    PostMessage(WM_INIT_CONNECTPORT);

    return TRUE;
}

BEGIN_MESSAGE_MAP(CDlgCDCheck, CDialog)
    ON_MESSAGE(WM_INIT_CONNECTPORT, OnInitConnectPort)
    ON_BN_CLICKED(ID_CD_OPEN, &CDlgCDCheck::OnBnClickedOpen)
    ON_BN_CLICKED(ID_CDSTATUS_REFRESH, &CDlgCDCheck::OnBnClickedRefresh)
    ON_BN_CLICKED(IDOK, &CDlgCDCheck::OnBnClickedOk)
	ON_WM_TIMER()
    ON_BN_CLICKED(IDC_RADIO_CD_NORMAL, &CDlgCDCheck::OnBnClickedRadioCdNormal)
    ON_BN_CLICKED(IDC_RADIO_CD_ERROR, &CDlgCDCheck::OnBnClickedRadioCdError)
    ON_BN_CLICKED(IDC_RADIO_CD_PASS, &CDlgCDCheck::OnBnClickedRadioCdPass)
END_MESSAGE_MAP()

// CDlgCDCheck 消息处理程序
LRESULT CDlgCDCheck::OnInitConnectPort(WPARAM wParam, LPARAM lParam)
{
    CString strStatus(_T("Invalid"));
    if (NULL == m_pCashdrawer)
    {
        MessageBox(_T("Cashdrawer was not configured or not exit!"), L"Warning", MB_ICONWARNING|MB_OK);
		Log(ERROR_LEVEL, EPOSChecker, _T("Cashdrawer was not configured or not exit!"));
		_QLOG_ERROR;
        SetDlgItemText(IDC_EDIT_CDSTATUS, strStatus);
        EnableOperateButtons(FALSE);
        return S_OK;
    }
    
    if (!m_pCashdrawer->Initialize())
    {
        MessageBox(_T("Fail to initialize cashdrawer, please check hardware connection!"), L"Warning", MB_ICONWARNING|MB_OK);
		Log(ERROR_LEVEL, EPOSChecker, _T("Failed to initialized cashdrawer!"));
		_QLOG_ERROR;
        SetDlgItemText(IDC_EDIT_CDSTATUS, strStatus);
        EnableOperateButtons(FALSE);
        return S_OK;
    }

    EnableOperateButtons();
    UpdateCDStatus(m_pCashdrawer->GetStatus());
    
    SetTimer(1, 500, NULL);

    return S_OK;
}

void CDlgCDCheck::OnBnClickedOpen()
{
    if (NULL != m_pCashdrawer)
    {
        m_pCashdrawer->Open();

		Sleep(100);

		UpdateCDStatus(m_pCashdrawer->GetStatus());
    }
}

void CDlgCDCheck::OnBnClickedRefresh()
{
    if (NULL != m_pCashdrawer)
    {
        UpdateCDStatus(m_pCashdrawer->GetStatus());
    }
}

void CDlgCDCheck::OnBnClickedOk()
{
    GetParent()->PostMessage(WM_PT_CD, m_nStatus);

    OnOK();
}

void CDlgCDCheck::EnableOperateButtons(BOOL bEnable /* = TRUE */)
{
    CWnd *pBtn = GetDlgItem(ID_CD_OPEN);
    ASSERT(NULL != pBtn);
    pBtn->EnableWindow(bEnable);

    pBtn = GetDlgItem(ID_CDSTATUS_REFRESH);
    ASSERT(NULL != pBtn);
    pBtn->EnableWindow(bEnable);
}

void CDlgCDCheck::UpdateCDStatus(int nStatus)
{
    CString strStatus;
    switch(nStatus)
    {
    case CD_STATUS_OPEN: 
        strStatus = _T("Open");
        break;
    case CD_STATUS_CLOSE:
        strStatus = _T("Closed");
        break;
    case CD_STATUS_UNKNOWN:
    default:
        strStatus = _T("Unkown status");
        break;
    }

    SetDlgItemText(IDC_EDIT_CDSTATUS, strStatus);
}

void CDlgCDCheck::OnTimer(UINT_PTR nIDEvent)
{
	
	if (NULL != m_pCashdrawer && nIDEvent == 1)
    {
        UpdateCDStatus(m_pCashdrawer->GetStatus());
    }

	CDialog::OnTimer(nIDEvent);
}


void CDlgCDCheck::OnBnClickedRadioCdNormal()
{
    m_nStatus = ES_Normal;
    GetDlgItem(IDOK)->EnableWindow();
}


void CDlgCDCheck::OnBnClickedRadioCdError()
{
    m_nStatus = ES_Exception;
    GetDlgItem(IDOK)->EnableWindow();
}


void CDlgCDCheck::OnBnClickedRadioCdPass()
{
    m_nStatus = ES_Pass;
    GetDlgItem(IDOK)->EnableWindow();
}
